package com.ishansong.gis.tool;

import com.mongodb.Block;
import com.mongodb.ServerAddress;
import org.bson.BsonDocument;
import org.bson.BsonString;

import java.util.Collections;
import java.util.List;

/**
 * Created by danqiang on 2017/7/12.
 * bench
 */
public class Bench {
    private Perf perf = new Perf();
    private Mongo mongo = null;

    private Bench(List<ServerAddress> seeds) {
        mongo = new Mongo(seeds);
    }

    void benchFindAll() {
        mongo.switchCollection("test", "");
//        mongo.switchCollection("ss_location", "");
        mongo.iterateWithCollectionName(new Block<BsonDocument>() {
            public void apply(BsonDocument doc) {
                perf.out();
            }
        }, "order_location");
    }

    private void benchFindOne() {
//        mongo.switchCollection("ss_location", "order_location");
        mongo.switchCollection("test", "order_location_2017_05_04");

        startThreads(32, "6264212685915803391");
//        startThreads(32, "6264999326016143820");
        System.out.println("thread num: 32");
    }

    private void startThreads(final int threadNum, final String oldId) {
        for (int i = 0; i < threadNum; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                    BsonDocument criteria = new BsonDocument("oldId", new BsonInt64(Long.parseLong(oldId)));
                    BsonDocument criteria = new BsonDocument("orderId", new BsonString("DH2017051617144002"));

                    for (int i = 0; i < 1000000 / threadNum; i++) {
                        mongo.find(criteria, new Block<BsonDocument>() {
                            @Override
                            public void apply(BsonDocument bsonDocument) {
                                perf.out();
                            }
                        });
                    }
                }
            }).start();
        }
    }

    public static void main(String[] args) {
//        Bench bench = new Bench(Arrays.asList(
//                new ServerAddress("mt1", 27017),
//                new ServerAddress("mt3", 27017)
//        ));
//        Bench bench = new Bench(Arrays.asList(
//                new ServerAddress("mt2", 27017)
//        ));
        Bench bench = new Bench(Collections.singletonList(
                new ServerAddress("localhost", 27017)
        ));
//        Bench bench = new Bench(Arrays.asList(
//                new ServerAddress("localhost", 6300),
//                new ServerAddress("localhost", 6301)
//        ));
        bench.benchFindOne();
    }
}
